<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>表单</title>
    <script src="lib/react/react.development.js"></script>
    <script src="lib/react/react-dom.development.js"></script>
    <script src="lib/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>
    <script type="text/babel">
      class HelloWorld extends React.Component {
        constructor(props) {
          super(props);
          this.handleSubmit = this.handleSubmit.bind(this);
          this.state = {
            username: "默认用户名",
            exp: "0",
          };
        }
        handleInputChange(prop, e) {
          this.setState({
            [prop]: e.target.value,
          });
        }

        handleSubmit(e) {
          // 阻止默认提交行为
          e.preventDefault();
          // 获取表单内容
          console.log(this.state);
        }

        render() {
          return (
            <div>
              <form action="/abc" onSubmit={this.handleSubmit}>
                姓名:{" "}
                <input
                  type="text"
                  name="username"
                  value={this.state.username}
                  onChange={(e) => this.handleInputChange("username", e)}
                />
                <br />
                经验:{" "}
                <select
                  name="exp"
                  value={this.state.exp}
                  onChange={(e) => this.handleInputChange("exp", e)}
                >
                  <option value="0">无</option>
                  <option value="1">1-3年</option>
                  <option value="2">3-5年</option>
                </select>
                <input type="submit" value="提交" />
              </form>
            </div>
          );
        }
      }

      ReactDOM.render(<HelloWorld />, document.getElementById('root'))
    </script>
  </body>
</html>
